unit uDataContainers;

interface

uses
  Classes;

const
  SEPARATOR = '=';
  PAIR_SEPARATOR = '|';
  
type
  TParamList = class(TStringList)
  private
    function GetParam(const AParamName: string): string;
    procedure SetParam(const AParamName, Value: string);
  public
    property Params[const AParamName: string]: string read GetParam write SetParam;
    procedure Rename(const AParamName, ANewParamName: string);
    procedure DeleteByName(const AParamName: string);
    procedure SafeDelete(const AString: string);
    function GetParams: string;
  end;

implementation

{ TParamList }

function TParamList.GetParam(const AParamName: string): string;
begin
  if IndexOfName(AParamName) > -1 then
    Result := Values[AParamName]
  else
    Result := '';
end;

procedure TParamList.Rename(const AParamName, ANewParamName: string);
begin
  if IndexOfName(AParamName) > -1 then
  begin
    Params[ANewParamName] := Params[AParamName];
    DeleteByName(AParamName);
  end;
end;

procedure TParamList.DeleteByName(const AParamName: string);
begin
  if IndexOfName(AParamName) > -1 then
  begin
    Delete(IndexOfName(AParamName));
  end;
end;

procedure TParamList.SetParam(const AParamName, Value: string);
begin
  if IndexOfName(AParamName) = -1 then
    Add(AParamName + SEPARATOR + Value)
  else
    Values[AParamName] := Value;
end;

function TParamList.GetParams: string;
var
  i: Integer;
begin
  Result := '';
  for i := 0 to Count - 1 do
  begin
    Result := Result +
      Names[i] + SEPARATOR +
      Values[Names[i]] + PAIR_SEPARATOR;
  end;
end;

procedure TParamList.SafeDelete(const AString: string);
begin
  if IndexOf(AString) > 0 then
    Delete(IndexOf(AString));
end;

end.

